import 'package:flutter/material.dart';

import '../../../generated/l10n.dart';
import '../../ble/protocol/AutoConnectUtil.dart';
import '../../ble/protocol/base/SourceInterface.dart';
import '../../ble/protocol/iledcolor/BleBean.dart';
import '../../ble/protocol/iledcolor/source/SourceManager.dart';
import '../base/BaseWidget.dart';
import '../base/TitleBarWidget.dart';
import '../source/ShowSourceWidget.dart';

class AddListPage extends StatefulWidget {
  const AddListPage({super.key});

  @override
  State<AddListPage> createState() => _AddListPageState();
}

class _AddListPageState extends State<AddListPage> {
  List<SourceInterface> data = [];

  //已经选中的id
  List<int> selectIds = [];

  Size getScreenSize() {
    var lastConnectBleBean = AutoConnectUtil.instance.getLastConnectBleBean();
    BleBean bleBean = BleBean(bean: lastConnectBleBean);
    return bleBean.getSize();
  }

  List<SourceInterface> getSourceList() {
    Size screenSize = getScreenSize();
    List<SourceInterface> list = [];
    var map = SourceManager.instance.getSource();
    for (var element in map.values) {
      if (element.screenSize == screenSize) {
        list.add(element);
      }
    }
    return list;
  }

  @override
  void initState() {
    data = getSourceList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BaseWidget(
      child: Column(
        children: [
          TitleBarWidget(
            title: S.of(context).str_id_35,
            rightWidget: [
              GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  Navigator.of(context).pop(selectIds);
                },
                child: Padding(padding: const EdgeInsets.only(right: 10), child: Icon(Icons.save_outlined)),
              ),
            ],
          ),

          Expanded(
            child: ListView.builder(
              itemCount: data.length,
              itemBuilder: (BuildContext context, int index) {
                var source = data[index];
                return GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    if (selectIds.contains(source.id)) {
                      selectIds.remove(source.id);
                    } else {
                      selectIds.add(source.id);
                    }
                    setState(() {});
                  },
                  child: Container(
                    margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                    decoration: BoxDecoration(
                      color: Colors.blueGrey,
                      border: Border.all(
                        color: selectIds.contains(source.id) ? Colors.red : Colors.transparent,
                        width: 3,
                      ),
                    ),
                    child: ShowSourceWidget(source: source),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
